package com.tian.service.impl;

import com.tian.entity.ExchangeCode;
import com.tian.enums.ExchangeCodeStatusEnum;
import com.tian.mapper.ExchangeCodeMapper;
import com.tian.message.UserUseCouponExchangeCodeMessage;
import com.tian.service.UserUseCouponExchangeCodeService;
import com.tian.util.RedisConstantPre;
import com.tian.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * {@code @description:} TODO
 *
 * @author tianwc 公众号：Java后端技术全栈
 * 在线刷题 1200+java面试题和1000+篇技术文章：<a href="https://woaijava.cc/">博客地址</a>
 * {@code @date:} 2024-11-09 14:54
 * {@code @version:} 1.0
 */
@Slf4j
@Service
public class UserUseCouponExchangeCodeServiceImpl implements UserUseCouponExchangeCodeService {
    @Resource
    private ExchangeCodeMapper exchangeCodeMapper;
    @Resource
    private RedissonClient redissonClient;

    @Override
    public boolean process(UserUseCouponExchangeCodeMessage userUseCouponExchangeCodeMessage) {
        String code = userUseCouponExchangeCodeMessage.getExchangeCode();
        ExchangeCode exchangeCode;
        //支付回调时，没有code，需要根据订单号查询兑换码
        if (StringUtil.isBlank(code)) {
            RBucket<Object> bucket = redissonClient.getBucket(RedisConstantPre.EXCHANGE_CODE_USING_KEY + code);
            if (bucket.isExists()) {
                bucket.delete();
            }
            exchangeCode = exchangeCodeMapper.selectByOrderId(userUseCouponExchangeCodeMessage.getOrderId());
            if (exchangeCode == null) {
                log.error("兑换码不存在，code:{}", userUseCouponExchangeCodeMessage.getExchangeCode());
                return false;
            }
            exchangeCode.setStatus(userUseCouponExchangeCodeMessage.getStatus());
            exchangeCode.setUpdateTime(new Date());
            exchangeCode.setOrderId(userUseCouponExchangeCodeMessage.getOrderId());
            exchangeCodeMapper.updateByPrimaryKey(exchangeCode);
            return true;
        }

        exchangeCode = exchangeCodeMapper.selectByCode(userUseCouponExchangeCodeMessage.getExchangeCode());
        if (exchangeCode == null) {
            // 兑换码不存在
            log.error("兑换码不存在，code:{}", userUseCouponExchangeCodeMessage.getExchangeCode());
            return true;
        }
        //数据库中兑换码状态不是未使用，则表示次优惠券
        if (exchangeCode.getStatus() == ExchangeCodeStatusEnum.EXPIRED.getCode()) {
            // 兑换码已过期
            log.error("此兑换码:{}已过期,status:{}", exchangeCode.getCode(), exchangeCode.getStatus());
            return true;
        }
        // 预支付 兑换码未使用，现在给他更新成使用中
        exchangeCode.setStatus(userUseCouponExchangeCodeMessage.getStatus());
        exchangeCode.setUpdateTime(new Date());
        exchangeCode.setUserId(userUseCouponExchangeCodeMessage.getUserId());
        exchangeCode.setOrderId(userUseCouponExchangeCodeMessage.getOrderId());
        exchangeCodeMapper.updateByPrimaryKey(exchangeCode);
        return true;
    }
}
